package com.gdpa.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpa.entity.TenancyArea;
import com.gdpa.entity.vo.ResponseData;
import com.gdpa.service.ITenancyAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 租赁区域表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/tenancy-area")
public class TenancyAreaController {
    @Autowired
    private ITenancyAreaService iTenancyAreaService;

    @RequestMapping("/getAreaList")
    public ResponseData list(Page page,String search){
        List<TenancyArea> list = iTenancyAreaService.getAreaBySearch(page,search);
        return ResponseData.okPage(page.getTotal(),list);
    }

    @RequestMapping("/getAreaIdAndName")
    public ResponseData list(){
        List<TenancyArea> list = iTenancyAreaService.getAreaList();
        return ResponseData.okData(list);
    }

    @RequestMapping("/deleteAreaById/{id}")
    public ResponseData delete(@PathVariable("id") Integer id){
        Boolean isSuccess = null;
        try {
            isSuccess = iTenancyAreaService.deleteAreaById(id);
        } catch (Exception e) {
            return ResponseData.failMsg("删除失败");
        }
        if(isSuccess){
            return ResponseData.okMsg("删除成功!");
        }
        return ResponseData.failMsg("删除失败");
    }

    @RequestMapping("/isSameName")
    public ResponseData isSameName(String areaName,Integer areaId){
        List<String> list = iTenancyAreaService.getSameArea(areaId);
        for (String s : list) {
            if(areaName.equals(s))
                return ResponseData.okData(true);
        }
        return ResponseData.okData(false);
    }

    @RequestMapping("/isAlikeName")
    public ResponseData isAlikeName(String areaName){
        List<String> list = iTenancyAreaService.getArea();
        for (String s : list) {
            if(areaName.equals(s))
                return ResponseData.okData(true);
        }
        return ResponseData.okData(false);
    }

    @RequestMapping("/updateAreaById")
    public ResponseData updateAreaById(TenancyArea tenancyArea){
        boolean isSuccess = iTenancyAreaService.updateAreaById(tenancyArea);
        if (isSuccess)
            return ResponseData.okMsg("修改成功");
        return ResponseData.failMsg("修改失败");
    }

    @RequestMapping("/addArea")
    public ResponseData addArea(TenancyArea tenancyArea){
        boolean isSuccess = iTenancyAreaService.addArea(tenancyArea);
        if (isSuccess)
            return ResponseData.okMsg("添加成功");
        return ResponseData.failMsg("添加失败");
    }


}
